package com.icbc.api;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.icbc.api.internal.util.internal.util.fastjson.JSON;
import com.icbc.api.internal.util.internal.util.fastjson.JSONObject;
import com.icbc.api.request.BcssMemberIdentityInfoQueryRequestV1;
import com.icbc.api.request.BcssMemberIdentityInfoQueryRequestV1.BcssMemberIdentityInfoQueryRequestBizV1;
import com.icbc.api.response.BcssMemberIdentityInfoQueryResponseV1;//需要引入/lib/icbc-api-sdk-cop-io.jar包
 
import com.icbc.api.utils.IcbcEncrypt;

public class BcssMemberIdentityInfoQueryTestV1 {
	
	private static final Log logger = LogFactory.getLog(BcssMemberIdentityInfoQueryTestV1.class);
 

    // MY_PRIVATE_KEY
    protected static final String MY_PRIVATE_KEY = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCYtg/yihqjvAaYxExVP8f788jPaXLiZjKc2ZbytaupNE9F1LENLCvOzmqytLOi68zwU4863mIY4sTkbXG9Aps7XtePAmB53PEGWRDnd2sjeyGYb5T+wh4T6nM2OJaenIFxQefnUyOjpSQO7WHF+NE0lxhjY65Y06FGL6BX6OakPXNVaYyv8MSBBZJnr0hX+dpVskJ9EcPainA/YKDjXtRiWVIvpO8oftifqikaLQ72a+8NbswYofAzaUFbGLEChbm330kqn5PlYRndz8uxPOWFDBK35nr/vg/fpgtCyh3gOWb/7scBrfPdGL9qJDGWVVK4bovF0DQZU1bcHkEOEmR7AgMBAAECggEAZkEKfqIg4mAlnqdaYX7avwdnABMgvR4nZ8V/ALfK4PrI1f5sK2DXWJv47/51hnMUABxDfOypjPZTVo0+KoyTOvbvqa+ywAbEUTbUTdk1oUVyomqcvtVmZHmJedX8bOpo9lhmGwwdghczBNfIr1JDa059Ixf9Bzd19tQvLo4h2fjMjQR33mRzb+oeen789Vvk0OyATyEGlKWdH96g2CmRA2jDe6n/opPrIhziLKc7tm9jnycnX0Z7Jy2N3vNsGi9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mQnIpiPEFAEDqRfNGAVvvH35yDetqewKi0l7OEceTMN1C6NPym3zStvSoQayjYV+eIcZERkx31KhtFu9clZKgRTyPjdKMIth/wBtPKjL/5+PYalLdomM4ONthrPgnkN4x4R0+D4+EBpXo8gNiAFsNwIDAQAB";
    
    protected static final String APP_ID = "10000000000003244079";
    protected static final String RSA = "RSA";
    protected static final String RSA2 = "RSA2";
    protected static final String AESKEY="BNzGHgSP6qvPep5+PosHAA==";
    protected static final String CORPID="2000001840";
    protected static final String URL="https://gw.open.icbc.com.cn/api/bcss/member/identityInfo/query/V1";

    
    public static void main(String[] args) throws Exception {
    	BcssMemberIdentityInfoQueryTestV1 a = new BcssMemberIdentityInfoQueryTestV1();
        a.test_cop();
    }

    public void test_cop() throws IcbcApiException {
        BcssMemberIdentityInfoQueryRequestV1 request = new BcssMemberIdentityInfoQueryRequestV1();
        request.setServiceUrl(URL);
        BcssMemberIdentityInfoQueryRequestBizV1 bizContent = new BcssMemberIdentityInfoQueryRequestBizV1();         
        bizContent.setCorpId("2000001840");
        bizContent.setClientTransNo(getDateTimeString());
        bizContent.setQueryType("3");
        String clientTransNo=getDateTimeString();
        JSONObject json=new JSONObject();
        json.put("dt", "1");//1为通过会员卡号查询
//        json.put("name", "**");//姓名
//        json.put("paymedium", "**");//银行卡号
        json.put("memCardNo", "0016230000000004");//会员卡号
        json.put("clientTransNo", clientTransNo);
        bizContent.setQrCode(encryptAesContentByKey1(AESKEY,json.toString()));
        // QrCode是aes加密信息  格式如下
        //{ \"dt\":\"1\", \"mobile\":\"\", \"validateCode\":\"\",  \"staffno\":\"\", \"name\":\"12121\", \"custcode\":\"23423423\", \"custsort\":\"1\", \"memCardNo\":\"232323232\"  }"
        bizContent.setClientType("3");
        bizContent.setTimeStamp(getTimeStamp());
            
        request.setBizContent(bizContent);
        DefaultIcbcClient client = new DefaultIcbcClient(APP_ID,RSA2,MY_PRIVATE_KEY, APIGW_PUBLIC_KEY);
        BcssMemberIdentityInfoQueryResponseV1  result = client.execute(request);
        
        logger.info(JSON.toJSONString(result));
    }
    
    public String getTimeStamp(){
    	Date day=new Date();    
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		return df.format(day);  
    }
    
    public String getDateTimeString(){
    	Date day=new Date();    
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss"); 
		return df.format(day);  
    }
    
    
    public String encryptAesContentByKey1(String aesKey, String content) {
        String result = null;
        try {
            result = IcbcEncrypt.encryptContent(content, "AES", aesKey, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return result;
    }
}